package com.formula.propertyshop.view.common;


import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.formula.propertyshop.R;
import com.formula.propertyshop.adapter.BSDPropertyTypeSpinnerAdapter;
import com.formula.propertyshop.model.BSDPropertyType;

public class ABSDCalculatorFragment extends Fragment{
	private Button btnCalculate;
	private EditText txtPrice;
	private TextView txtResult;
	private Spinner cbbType;
	private Spinner cbbLocation;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.absd_calculator, container, false);
    btnCalculate = (Button) rootView.findViewById(R.id.btnBSD);
    txtPrice = (EditText) rootView.findViewById(R.id.txtPrice);
    txtResult = (TextView) rootView.findViewById(R.id.txtResult);
    cbbType = (Spinner) rootView.findViewById(R.id.cbbType);
	cbbLocation = (Spinner) rootView.findViewById(R.id.cbbLocation);
	
	List<BSDPropertyType>types = new ArrayList<BSDPropertyType>();
	types.add(new BSDPropertyType("1st property", 1));
	types.add(new BSDPropertyType("2nd property", 2));
	types.add(new BSDPropertyType("3rd or more property", 3));
	BSDPropertyTypeSpinnerAdapter adapterType = new BSDPropertyTypeSpinnerAdapter(this.getActivity(), android.R.layout.simple_spinner_item, types);
	adapterType.setDropDownViewResource(R.layout.dropdown_item);
	cbbType.setAdapter(adapterType);
	
	List<BSDPropertyType>locations = new ArrayList<BSDPropertyType>();
	locations.add(new BSDPropertyType("Singapre Citizen", 1));
	locations.add(new BSDPropertyType("Permanent Resident", 2));
	locations.add(new BSDPropertyType("Other", 3));
	BSDPropertyTypeSpinnerAdapter adapterLocatoin = new BSDPropertyTypeSpinnerAdapter(this.getActivity(), android.R.layout.simple_spinner_item, locations);
	adapterType.setDropDownViewResource(R.layout.dropdown_item);
	cbbLocation.setAdapter(adapterLocatoin);
	
    btnCalculate.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(validateData()){
				int type = ((BSDPropertyType) cbbType.getSelectedItem()).getValue();
				int location = ((BSDPropertyType) cbbLocation.getSelectedItem()).getValue();
				int range = 0;
				if(type==1 && location==1)
					range = 0;
				else if(type==1 && location==2)
					range=5;
				else if(type==1 && location==3)
					range=15;
				else if(type==2 && location==1)
					range=7;
				else if(type==2 && location==2)
					range=10;
				else if(type==2 && location==3)
					range=15;
				else if(type==3 && location==1)
					range=10;
				else if(type==3 && location==2)
					range=10;
				else if(type==3 && location==3)
					range=15;
				double price = Double.parseDouble(txtPrice.getText().toString());
				double value = ((double)range/100)*price;
				txtResult.setText("ABSD: "+value);
			}
		}
	});


    return rootView;
	}
	private boolean validateData(){
	Animation animFadein = AnimationUtils.loadAnimation(this.getActivity(),R.anim.sequence_button);
	if(txtPrice.getText().toString().isEmpty()){
		Toast.makeText(this.getActivity(), R.string.msg_field_not_empty, Toast.LENGTH_SHORT).show();
		txtPrice.startAnimation(animFadein);
		return false;
	}
	return true;
}

}
